package org.victoria.market.service.imp;

import com.alibaba.fastjson.JSON;
import org.apache.thrift.TException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.victoria.market.api.ICtgAttribute;
import org.victoria.market.dao.DaoSupport;
import org.victoria.market.domain.CtgAttribute;

import java.util.List;

/**
 * Created by thanatos on 15-10-2.
 */
@Service
public class ICtgAttributeImp implements ICtgAttribute.Iface{

    @Autowired DaoSupport daoSupport;

    @Override
    @Transactional(rollbackFor = {TException.class, RuntimeException.class})
    public String save(String obj) throws TException {
        CtgAttribute atgattr = JSON.toJavaObject(JSON.parseObject(obj), CtgAttribute.class);
        daoSupport.getQueryHelper()
                .setNameSpace("CtgAttribute.saveOne")
                .setValue(atgattr)
                .dao()
                .save();
        return atgattr.getId().toString();
    }

    @Override
    @Transactional(rollbackFor = {TException.class, RuntimeException.class})
    public void deleteOne(long id) throws TException {
        daoSupport.getQueryHelper()
                .setNameSpace("CtgAttribute.deleteOne")
                .setValue(id)
                .dao()
                .deleteOne();
    }

    @Override
    public String find(long oid) throws TException {
        List<CtgAttribute> ctgattrs = daoSupport.getQueryHelper()
                .setNameSpace("CtgAttribute.findByCid")
                .setValue(oid)
                .dao()
                .find();
        return JSON.toJSONString(ctgattrs);
    }
}
